﻿using MyPrismDemo.ViewModels;
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyPrismDemo.Views
{
    /// <summary>
    /// ViewA.xaml 的交互逻辑
    /// </summary>
    public partial class ViewA : UserControl
    {
        private readonly IRegionManager regionManager;
        private readonly IContainerProvider containerProvider;
        private IRegion aRegion;
        private ViewAViewModel viewModel;
        public ViewA(IRegionManager regionManager, IContainerProvider containerProvider)
        {
            InitializeComponent();
            this.regionManager = regionManager;
            this.containerProvider = containerProvider;
            this.viewModel = DataContext as ViewAViewModel;
            InitializeRegion();

        }

        public void InitializeRegion()
        {
            RegionManager.SetRegionName(ARegion, "aRegion");
            RegionManager.SetRegionManager(ARegion, regionManager);
            aRegion = regionManager.Regions["aRegion"];
            AddViewToARegion("ChildViewA");
            AddViewToARegion("ChildViewB");
        }

        private void AddViewToARegion(string viewName)
        {
            if (aRegion.GetView(viewName) == null)
            {
                var view = containerProvider.Resolve<object>("ViewB");
                aRegion.Add(view, viewName);
            }
        }
    }
}
